package com.redis.topic;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.MessageListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @Description
 * @Author lizhilong
 * @Date 2020/12/6 12:24
 */
@Configuration
@Slf4j
public class TopicListener {
    @Resource
    private RedissonClient redssionClient;

    @Bean
    public RTopic testTopic() {
        RTopic testTopic = redssionClient.getTopic("testTopic");
        testTopic.addListener(TestEvent.class, new MessageListener<TestEvent>() {
            @Override
            public void onMessage(CharSequence charSequence, TestEvent testEvent) {
                log.info("收到消息：{}", JSONUtil.parse(testEvent).toString());
            }
        });
        return testTopic;
    }
}
